package com.huike.web.controller.clue;


import com.huike.clue.domain.TbClue;
import com.huike.clue.domain.dto.TbClueDto;

import com.huike.clue.domain.dto.TbClueGainDto;
import com.huike.clue.service.TbClueService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/clues/clue")
@Slf4j
public class TbClueController extends BaseController {

    @Autowired
    private TbClueService tbClueService;

    /**
     * 线索分页查询
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(TbClueDto dto){

        startPage();
        List<TbClue> list = tbClueService.selectClue(dto);
        return getDataTable(list);
    }

    /**
     * 线索池分页查询
     * @param dto
     * @return
     */
    @GetMapping("/pool")
    public TableDataInfo pool(TbClueDto dto){

        startPage();
        List<TbClue> list = tbClueService.selectCluePool(dto);
        return getDataTable(list);
    }

    /**
     * 根据id查询线索详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult select(@PathVariable Long id){

        TbClue tbClue = tbClueService.selectById(id);
        return AjaxResult.success(tbClue);
    }

    /**
     * 新增线索
     * @param tbClue
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody TbClue tbClue){

        tbClue.setCreateBy(getUsername());
        tbClue.setCreateTime(LocalDate.now());
        tbClue.setOwner(getUsername());   // 线索归属人就是系统操作者
        tbClue.setStatus(1);
        int rows = tbClueService.insertClue(tbClue);
        return toAjax(rows);
    }

    /**
     * 转伪线索
     * @param id
     * @param
     * @return
     */
    @PutMapping("/false/{id}")
    public AjaxResult report(@PathVariable Long id,@RequestBody TbClue clue){
        int rows = tbClueService.turnFalseClue(id,clue);
        return toAjax(rows);
    }

    /**
     * 批量捞取线索
     * @param ids
     * @return
     */
    @PutMapping("/gain")
    public AjaxResult gain(@RequestBody TbClueGainDto ids){
        int rows = tbClueService.gainClue(ids);
        return toAjax(rows);
    }


    /**
     * 转商机
     * @param id
     * @return
     */
    @PutMapping("/changeBusiness/{id}")
    public AjaxResult changeBusiness(@PathVariable Long id){
        int rows = tbClueService.changeBusiness(id);
        return toAjax(rows);
    }
}
